﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour {

	public bool isMusic;		//游戏背景音乐是否播放

	public GameObject BG;		//背景管理器

	public Button btn_Start;	//开始游戏按钮
	public Button btn_Level;	//关卡按钮
	public Button btn_Set;		//游戏设置按钮
	public Button btn_Help;		//游戏帮助按钮
	public Button btn_Exit;		//退出游戏按钮

	public Image panel_Level;	//关卡面板
	public Image panel_Help;	//帮助面板
	public Image panel_Set;		//设置面板

	public Image BGImage;		//背景图

	public Toggle musicSelect;	//游戏内背景音乐设置

	// Use this for initialization
	void Start () {
		BG = GameObject.FindGameObjectWithTag ("BG");
		BGImage.sprite =  BG.GetComponent<BGImage> ().OnGetBG ();
		if (PlayerPrefs.HasKey ("IsMusic")) {
			isMusic = bool.Parse (PlayerPrefs.GetString ("IsMusic"));
		} else {
			isMusic = true;
		}
		btn_Start.onClick.AddListener (()=>{
			SceneManager.LoadScene(2);
		});

		btn_Level.onClick.AddListener (()=>{
			panel_Level.gameObject.SetActive(true);
		});

		btn_Set.onClick.AddListener (()=>{
			panel_Set.gameObject.SetActive (true);
		});

		btn_Help.onClick.AddListener (()=>{
			panel_Help.gameObject.SetActive (true);
		});

		btn_Exit.onClick.AddListener (()=>{
			Application.Quit();
		});
	
	}
		
	void Update () {
		if (BG.GetComponent<AudioSource>().isPlaying) {
			if (!isMusic) {
				BG.GetComponent<AudioSource>().Stop ();
			}
		} else {
			if (isMusic) {
				BG.GetComponent<AudioSource>().Play ();
			}
		}
	}

	public void OnBtnHelpClose()
	{
		panel_Help.gameObject.SetActive (false);
	}

	public void OnBtnSetClose()
	{
		isMusic = musicSelect.isOn;
		PlayerPrefs.SetString ("IsMusic",isMusic.ToString());
		panel_Set.gameObject.SetActive (false);
	}

	public void OnBtnChangeBG()
	{
		BGImage.sprite = BG.GetComponent<BGImage> ().OnChangeBG ();
	}
}
